//
//  BaseViewController.m
//  MELive
//
//  Created by 曹曹 on 2016/10/18.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "BaseViewController.h"
#import "GB_LeftViewController.h"
#import "Macro.h"
#import "UIView+Extension.h"
#import "GB_FollowTableViewCell.h"
#import "GB_Follow.h"
#import "GB_FollowData.h"
#import "GB_FindBatch.h"
#import "GB_FindBatchData.h"
#import "AFNetworking.h"
#import "ZWYNetTool.h"
#import "UIImageView+WebCache.h"
#import "YYModel.h"
#import "Masonry.h"
#import "BaseLabel.h"
@interface BaseViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UIView *followView;
@property (nonatomic, strong) UIImageView *followImageView;
@property (nonatomic, strong) UIButton *followButton;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) GB_Follow *follow;
@property (nonatomic, strong) GB_FindBatch *findBatch;
@property (nonatomic, strong) UIImageView *tom;
@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    [self creatNavBar];
    _followView = [[UIView alloc] initWithFrame:CGRectMake(320 - 35, 0, 0, 0)];
    NSLog(@"33333333333333333333333%f", SCREEN_WIDTH);
    _followView.layer.masksToBounds = YES;
    _followView.layer.cornerRadius = 10;
    _followImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo_triangle"]];
    _followImageView.frame = CGRectMake(320 - 35, 56, 0, 0);
    _followImageView.alpha = 0.8;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
    [self.view addGestureRecognizer:tap];
//    _followButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    _followButton.frame = CGRectMake(SCREEN_WIDTH - 45, 25, 27, 27);
//    [_followButton setImage:[[UIImage imageNamed:@"navi_follow_icon"] imageWithRenderingMode:1] forState:UIControlStateNormal];
//    [self.navigationController.view addSubview:_followButton];
//    [_followButton addTarget:self action:@selector(didFollowAction:) forControlEvents:UIControlEventTouchUpInside];
    [self createTableView];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320 - 60, 370) style:UITableViewStyleGrouped];
    _tableView.alpha = 0.8;
    _tableView.layer.masksToBounds = YES;
    _tableView.layer.cornerRadius = 10;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor blackColor];
    [_tableView registerNib:[UINib nibWithNibName:@"GB_FollowTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"GB_FollowTableViewCell"];
    NSDictionary *followHeader = @{@"X-Authorization": @"aEVmfZHsZeS1mBE+4pAl//KasIaxvWXU,WbFDuiDblRn2Q3K1jv0n03nDjbf3fMD/1EIZphnpSKjl6myiik5tiObFREHn YNellAPgj6KSNItJy2vPNuTxfKhd8H7H16PbCQ+fX8RLJHWDTbbzQU2d987B 0O/v87nVRffl6fhPNqdtLM6i6JwrTzQLD5mPF1Vj"};
    NSString *followBody = @"appId=100003&data=%7B%22uid%22%3A203995915%2C%22otherUids%22%3A%5B%22200014962%22%2C%22202806850%22%2C%22100678108%22%2C%22101148251%22%2C%22203234758%22%2C%22101785650%22%2C%22100442285%22%5D%7D&sign=bf950fb5d7efc607e80d2b950afe062276c1e0777ff41423e8d75ee1dbf58178";
    [ZWYNetTool POST:@"http://me.yy.com:80/liveShow/elegantCubeIndex4FollowV20" andBody:followBody andBodyStyle:ZWYBodyString andHeader:followHeader andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _follow = [GB_Follow yy_modelWithJSON:result];
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
    }];
    NSDictionary *findBatchheader = @{@"X-Authorization": @"aEVmfZHsZeS1mBE+4pAl//KasIaxvWXU,WbFDuiDblRn2Q3K1jv0n03nDjbf3fMD/1EIZphnpSKjl6myiik5tiObFREHn YNellAPgj6KSNItJy2vPNuTxfKhd8H7H16PbCQ+fX8RLJHWDTbbzQU2d987B 0O/v87nVRffl6fhPNqdtLM6i6JwrTzQLD5mPF1Vj",@"Content-Type": @"application/x-www-form-urlencoded"};
    NSString *findBatchbody = @"appId=100003&data=%7B%22uid%22%3A203995915%2C%22otherUids%22%3A%5B%22200014962%22%2C%22202806850%22%2C%22100678108%22%2C%22101148251%22%2C%22203234758%22%2C%22101785650%22%2C%22100442285%22%5D%7D&sign=bf950fb5d7efc607e80d2b950afe062276c1e0777ff41423e8d75ee1dbf58178";
    [ZWYNetTool POST:@"http://me.yy.com:80/user/findBatch" andBody:findBatchbody andBodyStyle:ZWYBodyString andHeader:findBatchheader andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _findBatch = [GB_FindBatch yy_modelWithJSON:result];
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
    }];
 

}

- (void)createTableView {
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [UIView new];
    headerView.backgroundColor = [UIColor blackColor];
    GB_BaseTextLabel *titleLabel = [GB_BaseTextLabel new];
    [headerView addSubview:titleLabel];
    titleLabel.textColor = [UIColor whiteColor];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headerView.centerY).offset(0);
        make.left.mas_equalTo(headerView.mas_left).offset(10);
    }];
    if (section == 0) {
        titleLabel.text = [NSString stringWithFormat:@"我关注的直播(%ld)", _follow.data.count];
    } else {
        titleLabel.text = @"最近看过的直播";
    }
    return headerView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return _follow.data.count;
    } else {
        return _findBatch.data.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GB_FollowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GB_FollowTableViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blackColor];
    if (indexPath.section == 0) {
        if (_follow.data.count == 0) {
//            cell.noPlayLabel.text = @"33";
        } else {
            cell.nickLabel.text = _follow.data[indexPath.row].nick;
            [cell.headerUrlImageView sd_setImageWithURL:[NSURL URLWithString:_follow.data[indexPath.row].headerUrl]];
            cell.noPlayLabel.hidden = YES;
        }
        cell.finishedLabel.hidden = YES;
    } else {
        cell.nickLabel.text = _findBatch.data[indexPath.row].nick;
        [cell.headerUrlImageView sd_setImageWithURL:[NSURL URLWithString:_findBatch.data[indexPath.row].headerUrl]];
        cell.noPlayLabel.hidden = YES;
    }
    cell.headerUrlImageView.layer.masksToBounds = YES;
    cell.headerUrlImageView.layer.cornerRadius = 20;
    return cell;
}

- (void)click:(UITapGestureRecognizer *)tap {
    [UIView animateWithDuration:0.5 animations:^{
        _followImageView.frame = CGRectMake(320 - 35, 54, 0, 0);
        _followView.frame = CGRectMake(320 - 35, 64, 0, 0);
    }];
}


- (void)creatNavBar {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"navi_follow_icon"] imageWithRenderingMode:1] style:UIBarButtonItemStylePlain target:self action:@selector(didHeadAction:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"navi_follow_icon"] imageWithRenderingMode:1] style:UIBarButtonItemStylePlain target:self action:@selector(didFollowAction:)];

}

- (void)didHeadAction:(UIBarButtonItem *)head {
    GB_LeftViewController *vc = [[GB_LeftViewController alloc] initWithNibName:@"GB_LeftViewController" bundle:[NSBundle mainBundle]];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

#pragma mark - followView

- (void)didFollowAction:(UIBarButtonItem *)follow {
//    button.selected = !button.selected;
//    if ((button.selected = YES)) {
    NSMutableArray  *arrayM=[NSMutableArray array];
    for (int i=1; i<= 55; i++) {
        [arrayM addObject:[UIImage imageNamed:[NSString stringWithFormat:@"Me_icon_loading%d",i]]];
    }
//    self.tom = [[UIImageView alloc] initWithFrame:CGRectMake(20, 80, 100, 100)];
//    _tom.centerX = self.view.centerX;
//    _tom.centerY = self.view.centerY - 64;
//    [self.followView addSubview:self.tom];
//    //设置动画数组
//    [self.tom setAnimationImages:arrayM];
//    //设置动画播放次数
//    [self.tom setAnimationRepeatCount:0];
//    //设置动画播放时间
//    [self.tom setAnimationDuration:40 * 0.075];
//    //开始动画
//    [self.tom startAnimating];
    [self.navigationController.view addSubview:_followView];
    [_followView addSubview:_tableView];
    [self.navigationController.view addSubview:_followImageView];
    [UIView animateWithDuration:0.3 animations:^{
        _followView.frame = CGRectMake(50, 64, 320 - 60, 333);
        _followImageView.frame = CGRectMake(320 - 35, 54, 10, 10);
    }];
//    } else {
//        _followImageView.frame = CGRectMake(SCREEN_WIDTH - 40, 54, 0, 0);
//        _followView.frame = CGRectMake(SCREEN_WIDTH - 35, 64, 0, 0);
//    }
}



@end
